package com.faxsun.common.currency.domain;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.broadleafcommerce.common.currency.domain.BroadleafCurrency;
import org.broadleafcommerce.common.currency.domain.BroadleafCurrencyImpl;
import org.broadleafcommerce.common.presentation.AdminPresentation;
import org.broadleafcommerce.common.presentation.AdminPresentationClass;
import org.broadleafcommerce.common.presentation.AdminPresentationToOneLookup;
import org.broadleafcommerce.common.presentation.PopulateToOneFieldsEnum;
import org.broadleafcommerce.common.presentation.client.SupportedFieldType;
import org.broadleafcommerce.common.presentation.client.VisibilityEnum;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * 汇率表
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
@Entity
@Table(name = "FS_CURRENCY_RATE")
@AdminPresentationClass(populateToOneFields = PopulateToOneFieldsEnum.TRUE, friendlyName = "CurrencyRateImpl")
public class FSCurrencyRateImpl implements FSCurrencyRate {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4421666907526604128L;

	@Id
	@GeneratedValue(generator = "CurrencyRateId")
	@GenericGenerator(name = "CurrencyRateId", strategy = "org.broadleafcommerce.common.persistence.IdOverrideTableGenerator", parameters = {
			@Parameter(name = "segment_value", value = "CurrencyRateImpl"),
			@Parameter(name = "entity_name", value = "com.faxsun.common.currency.domain.FSCurrencyRateImpl") })
	@Column(name = "CURRENCY_RATE_ID")
	@AdminPresentation(friendlyName = "Currency_Rate_ID", visibility = VisibilityEnum.HIDDEN_ALL)
	protected Long id;

	@OneToOne(optional = false, targetEntity = BroadleafCurrencyImpl.class)
	@JoinColumn(name = "CURRENCY_CODE_SOURCE", unique = false, nullable = false, updatable = false)
	@AdminPresentation(friendlyName = "CurrencyRateImpl_Source", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.SOURCE, groupOrder = Presentation.Group.Order.General, prominent = true, gridOrder = 1000, fieldType = SupportedFieldType.BROADLEAF_ENUMERATION, broadleafEnumeration = Presentation.EnumString.Name)
	@AdminPresentationToOneLookup(lookupDisplayProperty = "friendlyName")
	protected BroadleafCurrency sourceCurrency;

	@OneToOne(optional = false, targetEntity = BroadleafCurrencyImpl.class)
	@JoinColumn(name = "CURRENCY_CODE_DEST", unique = false, nullable = false, updatable = false)
	@AdminPresentation(friendlyName = "CurrencyRateImpl_Dest", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.DEST, groupOrder = Presentation.Group.Order.General, prominent = true, gridOrder = 2000, fieldType = SupportedFieldType.BROADLEAF_ENUMERATION, broadleafEnumeration = Presentation.EnumString.Name)
	@AdminPresentationToOneLookup(lookupDisplayProperty = "friendlyName")
	protected BroadleafCurrency destinationCurrency;

	@Column(name = "RATE", nullable = false, updatable = true, unique = false)
	@AdminPresentation(friendlyName = "CurrencyRateImpl_Rate", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.RATE, groupOrder = Presentation.Group.Order.General, prominent = true, gridOrder = 3000)
	protected BigDecimal rate;

	@Column(name = "UPDATE_DATE")
	@AdminPresentation(friendlyName = "CurrencyRateImpl_Date", group = Presentation.Group.Name.General, order = Presentation.FieldOrder.DATE, groupOrder = Presentation.Group.Order.General, prominent = true, gridOrder = 4000)
	protected Date date;

	@Override
	public FSCurrencyEnum getSourceCurrency() {
		return FSCurrencyEnum.getInstance(sourceCurrency.getCurrencyCode());
	}

	@Override
	public void setSourceCurrency(FSCurrencyEnum source) {

		this.sourceCurrency = source.getBroadleafCurrency();

	}

	@Override
	public FSCurrencyEnum getDestinationCurrency() {
		return FSCurrencyEnum
				.getInstance(destinationCurrency.getCurrencyCode());
	}

	@Override
	public void setDestinationCurrency(FSCurrencyEnum destination) {
		this.destinationCurrency = destination.getBroadleafCurrency();
	}

	@Override
	public BigDecimal getRate() {
		return this.rate;
	}

	@Override
	public void setRate(BigDecimal rate) {
		this.rate = rate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public static class Presentation {
		public static class Tab {
			public static class Name {
				public static final String CurrencyRateItems = "CurrencyRate_Tab";
			}

		}

		public static class Group {
			public static class Name {
				public static final String General = "CurrencyRateImpl";
			}

			public static class Order {
				public static final int General = 1000;
			}
		}

		public static class FieldOrder {
			public static final int NAME = 1000;
			public static final int SOURCE = 2000;
			public static final int DEST = 3000;
			public static final int RATE = 4000;
			public static final int DATE = 5000;
		}

		public static class EnumString {
			public static final String Name = "com.faxsun.common.currency.domain.FSCurrencyEnum";
		}
	}

	public String getMainEntityName() {
		return this.sourceCurrency.getCurrencyCode() + "-"
				+ this.destinationCurrency.getCurrencyCode();
	}
}
